package com.itheima.mall.controller;

import com.itheima.mall.pojo.Demo;
import com.itheima.mall.pojo.FlashPromotion;
import com.itheima.mall.service.SmsFlashPromotionService;
import com.itheima.mall.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/flash")
public class SmsFlashPromotionController {

    @Autowired
    private SmsFlashPromotionService smsFlashPromotionService;


    /**
     * 添加活动
     */
    @PostMapping("/create")
    public ResponseEntity<String> create(@RequestBody FlashPromotion flashPromotion){
        smsFlashPromotionService.create(flashPromotion);
        return ResponseEntity.ok("ok");
    }

    /**
     * 删除活动
     */
    @PostMapping("/delete/{id}")
    public ResponseEntity<String> delete(@PathVariable Long id){
        smsFlashPromotionService.delete(id);
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 根据活动名称分页查询
     */
    @GetMapping("/list")
    public ResponseEntity<PageResult> list(String keyword, @RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "5") Integer pageSize){
        PageResult pageResult = smsFlashPromotionService.list(keyword,pageNum,pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 修改上下线状态
     */
    @PostMapping("/update/status/{id}")
    public ResponseEntity<String> updateStatus(@PathVariable Long id, Integer status) {
        smsFlashPromotionService.updateStatus(id,status);
        return ResponseEntity.ok("操作成功");
    }


    /**
     * 编辑活动
     */
    @PostMapping("/update/{id}")
    public ResponseEntity<String> update(@PathVariable Long id,@RequestBody FlashPromotion flashPromotion) {
        smsFlashPromotionService.update(id,flashPromotion);
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 获取活动详情
     * TODO 可能有问题
     */
    @GetMapping("/{id}")
    public ResponseEntity<FlashPromotion> flash(@PathVariable Long id){
        FlashPromotion flashPromotion = smsFlashPromotionService.flash(id);
        return ResponseEntity.ok(flashPromotion);
    }


}
